﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Workshop;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Workshop
{
    public class GetWorkShopListHandler : IRequestHandler<GetWorkShopListCommand, ApiPaging<WrokShopDTOS>>
    {
        private readonly IBaseRespository<Workshopmodel> work;
        private readonly IMapper mapper;

        public GetWorkShopListHandler(IBaseRespository<Workshopmodel> work,
            IMapper mapper)
        {
            this.work = work;
            this.mapper = mapper;
        }

        public Task<ApiPaging<WrokShopDTOS>> Handle(GetWorkShopListCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<WrokShopDTOS> res=new ApiPaging<WrokShopDTOS>();

            var model = work.GetAll().Where(x=>!x.Isdel);

            var linq = from a in model
                       select new WrokShopDTOS
                       {
                           Id = a.Id,
                           ShopNum = a.ShopNum,
                           Workshopcode = a.Workshopcode,
                           Workshopdesc = a.Workshopdesc,
                           Workshopname = a.Workshopname,
                       };
            res.PageDatas = linq.OrderBy(x=>x.Id).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            res.Code = ApiEnum.Success;
            
            res.Msg = "查询成功";
            return Task.FromResult(res);
        }
    }
}
